/*
   Copyright 2009 Bloo. All Rights Reserved.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package org.youtensils.www.client.widgets;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;

/**
 * A progress bar widget.
 * @author Bobby Soares
 */
public class ProgressBar extends Composite  {
	private int progress = 0;
	private HorizontalPanel progressBox, progressFill;
	/**
	 * The default constructor.
	 */
	public ProgressBar(){
		progressBox = new HorizontalPanel();
		progressBox.setWidth("100%");
		progressBox.setHeight("100%");
		progressFill = new HorizontalPanel();
		progressFill.add(new HTML());//add filler
		progressFill.setStyleName("gwt-ProgressBar-Fill");
		progressFill.setHeight("100%");
		progressBox.add(progressFill);
    	initWidget(progressBox);
    	setStyleName("gwt-ProgressBar");
	}
	/**
	 * Sets the progress information.
	 * @param loaded a numeric value representing how much has loaded
	 * @param total a numeric value representing how much is loading
	 */
	public void setProgress(int loaded, int total){
		int per = (total == 0 ? 0 : Math.round(loaded*100/total));
		setProgress(per);
	}
	/**
	 * Sets the percentage progress information.
	 * @param percentage the progress percentage
	 */
	public void setProgress(int percentage){
		if(percentage > 100) percentage = 100;
		if(percentage < 0) percentage = 0;
		this.progress = percentage;
		progressFill.setWidth(percentage + "%");
	}
	/**
	 * Retrieves the progress percentage.
	 * @return the progress percentage
	 */
	public int getProgress(){
		return this.progress;
	}
}
